import time
import os
import csv
class WriteResult(object):
    default_path=r'E:\woniu\woniusales_ui_test_ddt\log'
    def __init__(self):
        self.filename = os.path.join(self.default_path,time.strftime("%Y%m%d%H%M%S"))  # 根据时间记录文本

    def write_header(self,fieldn):  # 写头部
        with open(self.filename, "a", encoding="utf8", newline="") as f:
            dict_writer = csv.DictWriter(f, fieldnames=fieldn)
            dict_writer.writeheader()

    def write_data_row(self, data):
        # 遇到的问题： 有换行符，在文本中写入时会换行。
        w_data = data.copy()  # 拷贝一份，不要对原数据进行修改
        for k, v in w_data.items():
            w_data[k] = v.replace("\n", "\\n")
        """只写数据"""
        with open(self.filename, "a", encoding="utf8", newline="") as f:
            fieldn = w_data.keys()
            dict_writer = csv.DictWriter(f, fieldnames=fieldn)
            dict_writer.writerow(w_data)


